package com.cj.yudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cj.yudada.model.dto.question.QuestionContentDto;
import com.cj.yudada.model.entity.App;
import com.cj.yudada.model.entity.Question;
import com.cj.yudada.model.entity.ScoringResult;
import com.cj.yudada.model.entity.UserAnswer;
import com.cj.yudada.model.vo.QuestionVO;
import com.cj.yudada.model.vo.ScoringResultVO;
import com.cj.yudada.service.AppService;
import com.cj.yudada.service.QuestionService;
import com.cj.yudada.service.ScoringResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测评类应用
 * @author cc
 * @date 2024-11-10 18:15
 **/
@ScoringStrategyConfig(appType = 0,scoringStrategy = 0)
public class CustomTestScoringStrategyImpl implements ScoringStrategy {
    @Autowired
    AppService appService;

    @Autowired
    QuestionService questionService;

    @Autowired
    ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) {

        Long appId = app.getId();

        Question question = questionService.getOne(Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId));

        List<ScoringResult> scoringResultList = scoringResultService.list(Wrappers.lambdaQuery(ScoringResult.class).eq(ScoringResult::getAppId, appId));

        // 计算选项，， I   E   分别有几个    ====>   I：10  E：5  ==》 scorintResult的时候  result_prop里面的 I E  累加分数
        Map<String, Integer> optionCount = new HashMap<>();


        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDto> questionContent = questionVO.getQuestionContent();

        for (QuestionContentDto questionContentDto : questionContent) {

            for (String choice : choices) {

                for (QuestionContentDto.Option option : questionContentDto.getOptions()) {
                    if (choice.equals(option.getKey())){
                        // 匹配的值
                        String result  = option.getResult();

                        // 统计累加
                        if (!optionCount.containsKey(result)) {
                            optionCount.put(result, 1);
                        } else {
                            optionCount.put(result, optionCount.get(result) + 1);
                        }
                    }
                }

            }
        }



        //  累加看  哪一个结果 命中的分最高

        // 初始化最高分数， 和  最高分数对应的评分结果
        int maxScore = 0 ;
        ScoringResult maxScoringResult = scoringResultList.get(0);




        // 遍历评分结果
        for (ScoringResult scoringResult : scoringResultList) {

            // 计算当前结果  的评分
            List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);

            // 根据用户选项计算出来的 optionCount 权重
            int score = resultProp.stream().mapToInt(prop -> optionCount.getOrDefault(prop, 0)).sum();

            if (score > maxScore) {
                maxScore = score;
                maxScoringResult = scoringResult;
            }

        }


        // 构造返回值
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        // 测评类应用  不用填得分
//        userAnswer.setResultScore(maxScore);



        return userAnswer;
    }
}
